//
//  LNHomeViewController.m
//  LNDevelopExample
//
//  Created by wwww on 2018/9/7.
//  Copyright © 2018年 淘发现1iOS. All rights reserved.
//

#import "LNHomeViewController.h"
#import "LNNavigationController.h"
#import "LNHomeViewModel.h"
#import "LNFocusTableViewCell.h"
#import "LNFoodsViewCell.h"
#import "LNHomeThemeCell.h"
#import "LNHomeThemeHeadView.h"
#import "LNSearchBar.h"
@interface LNHomeViewController ()

@property (nonatomic,strong) LNHomeViewModel *viewModel;

@property (nonatomic,strong) LNSearchBar *searchBar;
@end

@implementation LNHomeViewController
@dynamic viewModel;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:LNColorAlpha(255, 255, 255, 0.01)] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // UI
    [self setupUI];
    // 导航栏
    [self setupNavigationItem];
    //注册Cell
    [self registerNibCell];
    
    self.collectionView.hidden = YES;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    btn.frame = CGRectMake(100, 100, 100, 40);
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

-(void)test{
    
    [self.viewModel.testCommand execute:nil];
}

#pragma mark - Override
-(void)bindViewModel{
    [super bindViewModel];
}

#pragma mark - 初始化UI
-(void)setupUI{
    
    
}

#pragma mark - 设置导航栏
-(void)setupNavigationItem{
    
    self.navigationItem.titleView = self.searchBar;
}

#pragma mark - 注册Cell
-(void)registerNibCell{
    
    [self.collectionView ln_registerCell:LNFocusTableViewCell.class];
    [self.collectionView ln_registerCell:LNHomeThemeCell.class];
    [self.collectionView ln_registerNibCell:LNFoodsViewCell.class];
    
    [self.collectionView ln_registerCell:LNHomeThemeHeadView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader];
}
#pragma mark - 事件处理


#pragma mark - Override
/// 配置tableView的区域
- (UIEdgeInsets)contentInset{
    return UIEdgeInsetsMake(0, 0, LN_APPLICATION_TAB_BAR_HEIGHT, 0);
}

#pragma mark - 重写父类方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 2 || section == 3){
        return [self.viewModel.dataSource[section] count];
    }
    else
        return 1;
}

/// 返回自定义的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger section = indexPath.section;
    if (section == 0) {
         return [collectionView dequeueReusableCellWithReuseIdentifier:@"LNFocusTableViewCell" forIndexPath:indexPath];
    }
    else if(section == 1) {
        return [collectionView dequeueReusableCellWithReuseIdentifier:@"LNHomeThemeCell" forIndexPath:indexPath];
    }
    else {
         return [collectionView dequeueReusableCellWithReuseIdentifier:@"LNFoodsViewCell" forIndexPath:indexPath];
    }
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger section = indexPath.section;
    if(kind == UICollectionElementKindSectionHeader){
        
        if (section == 2 || section == 3) {
             LNHomeThemeHeadView *headView = [collectionView ln_dequeueReusableWithHeadView:LNHomeThemeHeadView.class forIndexPath:indexPath];
             headView.titleLabel.text = section==2?@"新秀菜谱":@"最近流行";
             return headView;
        }
    }
    return nil;
}

/// 设置SectionHeadView的大小
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    if(section == 2 || section== 3)
        return CGSizeMake(LN_SCREEN_WIDTH, 44);
    else
        return CGSizeMake(LN_SCREEN_WIDTH, 0);
}

//定义每个UICollectionView 的边距
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  
    NSInteger section = indexPath.section;
    
    if(section == 0)
        return CGSizeMake(LN_SCREEN_WIDTH, 220);
    else if (section == 1)
        return CGSizeMake(LN_SCREEN_WIDTH, 90);
    else
        return CGSizeMake((LN_SCREEN_WIDTH-15)/2, kHeight(180));
}

//定义每个UICollectionView 的边距
-( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section
{
    if (section == 2 || section == 3) {
        return UIEdgeInsetsMake(5, 5, 5, 5);//分别为上、左、下、右
    }
    else
        return UIEdgeInsetsMake(0, 0, 0, 0);//分别为上、左、下、右
}

/// 绑定数据
- (void)configureCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object{
    
    NSInteger section = indexPath.section;
    if (section == 0) {
        [(LNFocusTableViewCell*)cell bindViewModel:object];
    }
    else if (section == 2 || section == 3){
         [(LNFoodsViewCell*)cell bindViewModel:object[indexPath.row]];
    }
}

#pragma mark - setter and getter
-(LNSearchBar *)searchBar{
    
    if(_searchBar == nil){
        _searchBar = [[LNSearchBar alloc] initWithFrame:CGRectMake(0, 0, LN_SCREEN_WIDTH-80, 30)];
        _searchBar.placeholder = @"搜索食材、菜谱";
    }
    return _searchBar;
}
@end
